

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("waterway");  //Set current page
	App.init(); //Initialise plugins and elements

	//绑定机构下拉列表
	bindOrg2('org');

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增水道
	$('#addDict').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("水道新增");
		$("#modal").modal('show');
	});
	
	//批量删除
	$('#batchDelete').click(function(){
		
		var data = $('#table').bootstrapTable('getSelections');
		if(data.length <= 0){
			window.wxc.xcConfirm('请勾选要<strong>批量删除</strong>的水道数据', window.wxc.xcConfirm.typeEnum.info);
			return;
		}
		var waterwaycodes = "";
		for(var i = 0; i < data.length; i++){
			waterwaycodes += data[i].waterwaycode + ",";
		}
		
		waterwaycodes = waterwaycodes.substring(0, waterwaycodes.length - 1);

		window.wxc.xcConfirm('是否<strong>删除</strong>水道数据?<br /><span style="color:red;">['+ waterwaycodes +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'waterway/batchDelete.do',
					data: {"waterwaycodes": waterwaycodes},
					dataType: "json",
					success: function(data){	          						
						if(data.result == 1){

							window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success,{
								onOk:function(v){
									$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
								}
							});
						}
						else{
							window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.error);
						}
			      	}
				});
			}
		});
	})
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDict").on("hidden.bs.modal", function() {
		$('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').remove();	
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
		    '   <td colspan="7" style="font-size:14px;">			'+
			'		单充底部“新增行”增加水道！		'+
			'	</td>						'+
			'</tr>';
			
			$('#' + tableID).find('tbody').append(trStr);
		}
		
		$(this).find('[type="text"]').val('');
		$(this).find('[type="checkbox"]').prop("checked",true);         	
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'waterway/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	waterwaycode: $('#waterwaycode').val(),
                	waterwayname: $('#waterwayname').val(),
                	orgcode: $('#org').attr('data')
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'waterwaycode',
	        title: '水道代码'
	    }, {
	        field: 'waterwayname',
	        title: '水道名称'
	    }, {
	        field: 'orgname',
	        title: '所属机构'
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '300px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [
        '<button type="button" class="Mark btn btn-inverse  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-tower"></span>航标</button>',
        '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
        '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
    ].join('');
}

window.operateEvents = {

    'click .Mark': function (e, value, row, index) {
		var waterwaycode = row.waterwaycode;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "mark.html?waterwaycode=" + waterwaycode);
    	
		$('#modalTile').html("航标管理");
		$("#modal").modal('show');
    },
    'click .Edit': function (e, value, row, index) {
		var waterwaycode = row.waterwaycode;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "edit.html?waterwaycode=" + waterwaycode);
    	
		$('#modalTile').html("水道修改");
		$("#modal").modal('show');
    },
    'click .Delete': function (e, value, row, index) {

		var waterwaycode = row.waterwaycode;
		

		window.wxc.xcConfirm('是否<strong>删除</strong>水道数据?<br /><span style="color:red;">['+ waterwaycode +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'waterway/delete.do',
					data: {"waterwaycode": waterwaycode},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){

							window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success,{
								onOk:function(v){
									$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
								}
							});
						}
						else{
							window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.error);
						}
			      	}
				});
			}
		});
    }
 };

